package com.winit.common.util;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigUtil {

	public static final String ROOT_PATH = "rootPath";
	public static final String DEV_MODEL = "dev_model";
	public static final String APPLOG_PATH = "applog_path";

	private static Properties prop;
    private static final  String propFile="config.properties";
    private static final Logger log = LoggerFactory.getLogger(ConfigUtil.class);

    private ConfigUtil(){
    	
    }
    
    /**
     * 初始化配置文件 
     */
    public static synchronized  void initPropertFile(){
    	if(prop==null){
    		prop = new Properties();
    		try {
    			InputStream ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(propFile);
    			prop.load(new InputStreamReader(ins,"UTF-8"));
    		} catch (Exception e) {
    			log.error("加载Config.properties 文件出错:",e);
    		}
    	}
    }

	public static void reloadPropertFile(){
		try {
			prop = new Properties();
			InputStream ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(propFile);
			prop.load(new InputStreamReader(ins,"UTF-8"));
		} catch (Exception e) {
			log.error("加载Config.properties 文件出错:",e);
		}
	}

	/**
	 * @param value
	 * @return
	 */
	public synchronized static String getValue(String value){
		ConfigUtil.initPropertFile();
		return prop.getProperty(value);
	}
	
	public synchronized static void setValue(String key, String value){
		ConfigUtil.initPropertFile();
		prop.setProperty(key,value);
	}

}
